package vip.xumy.admin.um.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import vip.xumy.admin.um.mapper.IPermissionMapper;
import vip.xumy.admin.um.pojo.Menu;
import vip.xumy.admin.um.pojo.Permission;
import vip.xumy.admin.um.service.IMenuService;
import vip.xumy.admin.um.service.IPermissionService;
import vip.xumy.admin.um.service.IPermissionValidationService;
import vip.xumy.core.exception.CoreException;
import vip.xumy.core.pojo.com.Entry;
import vip.xumy.core.utils.StringUtil;

@Service
@Transactional
public class PermissionService implements IMenuService,IPermissionValidationService,IPermissionService{
	@Autowired
	private IPermissionMapper permissionMapper;

	@Override
	public List<Entry<String, String>> queryAllModule(String userId) {
		return permissionMapper.queryAllModule(userId);
	}
	
	@Override
	public List<Menu> queryMenuTree(String userName, String perParent) {
		if(StringUtils.isEmpty(userName)){
			return new ArrayList<>();
		}
		return permissionMapper.queryMenuTree(userName, perParent);
	}

	@Override
	public List<String> queryNodeContent(String userId, String parentContent) {
		return permissionMapper.queryContentByParentContent(userId,parentContent);
	}

	@Override
	public List<String> queryCodeByContent(String resContent) {
		return permissionMapper.queryCodeByContent(resContent);
	}
	
	@Override
	public boolean havePermission(String userId, String resCode) {
		long l = permissionMapper.havePermission(userId, resCode);
		return l > 0L;
	}

	@Override
	public List<Permission> queryPermissionTree(boolean isOperater) {
		if(isOperater) {
			return permissionMapper.queryPermissionTree();
		}
		return permissionMapper.queryPublicPerTree();
	}

	@Override
	public void save(Permission permission) throws CoreException{
		setDefault(permission);
		if(permissionMapper.isExist(permission.getId()) > 0L) {
			throw new CoreException("权限编号已经存在，请修改");
		}
		permissionMapper.insertOne(permission);
	}

	private void setDefault(Permission permission) throws CoreException {
		if(StringUtil.isEmpty(permission.getId(),permission.getName())) {
			throw new CoreException("权限编号，权限名称不能为空");
		}
		if(permission.getDesc() == null) {
			permission.setDesc("");
		}
		if(StringUtil.isEmpty(permission.getPerContent())) {
			permission.setPerContent(permission.getName());
		}
		if(StringUtil.isEmpty(permission.getPerStatus())) {
			permission.setPerStatus("A");
		}
	}

	@Override
	public void update(Permission permission) throws CoreException {
		permissionMapper.updateByCode(permission);
	}

	@Override
	public void delete(String code) {
		permissionMapper.deleteByCode(code);
	}

	@Override
	public List<String> myPermissions(String userName, String parentContent) {
		return permissionMapper.queryContentByParentContent(userName, parentContent);
	}

}
